/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.example.demo.user;
import com.alibaba.fastjson.JSON;
import com.example.demo.common.ApiResponse;
import com.example.demo.product.Product;
import com.example.demo.product.ProductRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
/**
 *
 * @author Administrator
 */
@RestController
public class UserController {
    
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private ProductRepository productRepository;
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);    
    @RequestMapping("/user")
    public ResponseEntity<?> index(@RequestParam(value = "id",defaultValue = "1") Long Id) {
          ArrayList<Long> ids  = new ArrayList();
          ids.add(1l);
          ids.add(2l);
          Iterable<User> lists = userRepository.findAllById(ids);
          Optional<User> user =  userRepository.findById(Id);
          LOG.info(JSON.toJSONString(user));
          LOG.debug("123");          
        String code = redisTemplate.opsForValue().get("abc");
        LOG.info(code);
   
        List<Product> lists2 =  productRepository.findAll();
        return new ResponseEntity(ApiResponse.success(lists),HttpStatus.OK);
    }
    @PostMapping("/user")
    public ResponseEntity<?>user(@RequestBody User user){
        return new ResponseEntity(user,HttpStatus.OK);
    }
    @PostMapping("/user/form")
    public ResponseEntity<?>user(@RequestParam(value = "username") String username,@RequestParam(value="id") Long Id){
        return new ResponseEntity(username+":"+Id,HttpStatus.OK);
    }
}
